﻿using System;
using System.Linq.Expressions;
using System.Reflection;
using Dsl.Ped.Framework.Members;
using Dsl.Ped.Framework.Extensions;

namespace Dsl.Ped.Framework.Helpers
{
    /// <summary>
    /// Helper for the <see cref="Expression"/> types
    /// </summary>
    public class ExpressionHelper
    {
        public static Member GetMember<T>(Expression<Func<T>> expr)
        {
            var memberExpr = expr.Body as MemberExpression;

            if (memberExpr == null || memberExpr.Member.MemberType != MemberTypes.Property)
            {
                var message = string.Format(
                    "The given expression '{0}' does not represent a property of {0}.",
                    memberExpr.ToString());
                throw new ArgumentException(message);
            }

            return memberExpr.Member.ConvertToMember();
        }
    }
}
